(function($){
	
	var KEY = "jforest.modal";

	var Modal = function(options){
		this.ajax = $.extend({
			url:null,
			dataType:'json',
			dataFormat:function(params){
				return $.extend({},params || {});
			},
			callback:function(result){
				return result;
			}
		},options.ajax);
		delete options.ajax;
	
		this.options = $.extend({
			id:null,
			title:'&nbsp;',
			callback:function(result){}
		},options);
		
		this.$el = $('<div class="modal fade" tabindex="-1" id="'+this.options.id+'">'
				+'<div class="modal-dialog"><div class="modal-content">'
					+'<div class="modal-header"><button type="button" class="close" data-dismiss="modal">&times;</button>'
						+'<h4 class="modal-title">'+this.options.title+'</h4>'
					+'</div>'
					+'<div class="modal-body"></div>'
				+'</div>'
			+'</div></div>');
		if(this.ajax.url){
			var self = this;
			this.load(this.options.title,this.ajax.url,this.ajax.dataFormat(),self.ajax.callback);
		}else if(this.options.html){
			this.$el.find('.modal-body').html(this.options.html);
		}
		
		this.$el.data('callback',this.options.callback);
	}
	
	Modal.prototype = {
		show:function(){
			this.$el.modal('show');
		},
		hide:function(){
			this.$el.modal('hide');
		},
		load:function(title,url,data,callback){
			this.$el.find('.modal-title').html(title);
			if($.isFunction(callback)){
				var self = this;
				this.$el.find('.modal-body').load(url,data,function(){
					callback.apply(self);
				});
			}else{
				this.$el.find('.modal-body').load(url,data);
			}
		}
	}
	
	var ModalManager = function(element,options){
		this.$el = $(element);
		this.modalMap = {};
		if(options){this.addModal(options);}
	}
	
	ModalManager.prototype = {
		addModal:function(options){
			var modal = this.modalMap[options.id];
			if(!modal){
				modal = new Modal(options);
				this.modalMap[options.id] = modal;
				this.$el.append(modal.$el);
			}
			return modal;
		},
		getModal:function(id){
			return this.modalMap[id];
		}
	}
	
	$.fn.jfModal = function(){
		var args = Array.prototype.slice.call(arguments, 0);
		var value = undefined;
		this.each(function () {
			var $this = $(this);
			var entity = $this.data(KEY);
			if(!entity){
				entity = new ModalManager(this); //创建控件
				$this.data(KEY,entity); 
			}
	
			if (args.length === 0 || typeof(args[0]) === "object") {
				opts = args.length === 0 ? {} : $.extend({}, args[0]);
				value = entity.addModal(opts);
			}else if (typeof(args[0]) === "string"){
				if(args.length == 1){
					value = entity.getModal(args[0]);
					if(!value){
						value = entity.addModal({id:args[0]});
					}
				}else if(args[0] == 'getModal'){
					value = entity.getModal(args[1]);
					if(!value){
						value = entity.addModal({id:args[1]});
					}
				}else{
					var modal = entity.getModal(args[0]);
					if(!modal){
						modal = entity.addModal({id:args[0]});
					}
					value = modal[args[1]].apply(modal, args.slice(2));
				}
			}else{
				 throw 'Invalid arguments to '+KEY+' plugin: ' + args;
			}
	    });
	    return (value === undefined) ? this : value;
	};
	
	$.fn.jfModal.Constructor = ModalManager;
	
})(window.jQuery);